#### installing packages if not installed ####

list.of.packages <- c('tidyverse',
                      'ggplot2',
                      'lubridate',
                      'estimatr',
                      'ggthemes',
                      'readr',
                      'readxl',
                      'rdrobust',
                      'gridExtra')
new.packages <- list.of.packages[!(list.of.packages %in% installed.packages()[,"Package"])]
if(length(new.packages)) install.packages(new.packages)

#### attaching libraries ####

suppressPackageStartupMessages(
  
  {
    
    library(tidyverse)
    library(ggplot2)
    library(lubridate)
    library(estimatr)    
    library(ggthemes)
    library(readr)
    library(readxl)
    library(rdrobust)
    library(gridExtra)
    
  }
  
)

#### loading in crime data: aus #### 

load(file = "data/crimeatx.RData")

crime_aus = crime_atx %>% 
  mutate(crime_person = ifelse(crime_category == "person", 1, 0),
         crime_property = ifelse(crime_category == "property", 1, 0),
         crime_society = ifelse(crime_category == "society", 1, 0)) %>% 
  group_by(date) %>% 
  summarize(blm = mean(blm, na.rm = TRUE),
            crime_person = sum(crime_person, na.rm = TRUE),
            crime_property = sum(crime_property, na.rm = TRUE),
            crime_society = sum(crime_society, na.rm = TRUE)) 

crime_aus$blmrv = 
  crime_aus$date - min(crime_aus$date[crime_aus$blm == 1])

#### loading in crime data: los angeles ####

# crime

load("data/crimedat_la.RData")

# cleaning data

# crime 

crime_la = crime_f %>% 
  mutate(date = as.Date(date)) %>% 
  mutate(crime_violent = ifelse(crime_type == "violent", 1, 0),
         crime_property = ifelse(crime_type == "property", 1, 0),
         crime_society = ifelse(crime_type == "society", 1, 0)) %>% 
  dplyr::group_by(date) %>% 
  summarize(crime_violent = sum(crime_violent),
            crime_property = sum(crime_property),
            crime_society = sum(crime_society),
            blm = mean(blm)) 

crime_la$blmrv = 
  crime_la$date - min(crime_la$date[crime_la$blm == 1])

#### loading in crime data: philadelphia ####

load(file = "data/crimeph.RData")

crime_phi = ph_crime_df %>% 
  mutate(date = as.Date(dispatch_date)) %>% 
  dplyr::group_by(date) %>% 
  summarize(crime_violent = sum(crime_violent),
            crime_property = sum(crime_property),
            crime_society = sum(crime_society),
            blm = mean(blm)) 

crime_phi$blmrv = 
  crime_phi$date - min(crime_phi$date[crime_phi$blm == 1])

#### loading in crime data: seattle ####

load(file = "data/crimedata.RData")

crime = 
  crime %>% 
  mutate(date = as.Date(paste(substring(report_date, 7, 10),
                              substring(report_date, 1, 2),
                              substring(report_date, 4, 5), sep = "-"))) %>% 
  mutate(blm = ifelse(date >= as.Date("2020-05-29"), 1, 0)) %>% 
  mutate(cat_society = ifelse(crime_against_category == "SOCIETY", 1, 0),
         cat_property = ifelse(crime_against_category == "PROPERTY", 1, 0),
         cat_person = ifelse(crime_against_category == "PERSON", 1, 0),
         cat_notcrime = ifelse(crime_against_category == "NOT_A_CRIME", 1, 0)) %>% 
  filter(offense != "Identity Theft")

crime_sea = crime %>% 
  group_by(date) %>% 
  summarize(cat_person = sum(cat_person),
            cat_property = sum(cat_property),
            cat_society = sum(cat_society), 
            blm = mean(blm))

crime_sea$blmrv = 
  crime_sea$date - min(crime_sea$date[crime_sea$blm == 1])


#### generating descriptive plot #### 

# austin

crimeplot1 = crime_aus %>% 
  filter(date >= as.Date("2020-03-23")) %>%
  filter(blmrv <= 67) %>% 
  ggplot() + 
  geom_point(aes(x = date, y = crime_society),
             size = .3,
             alpha = .3) + 
  geom_smooth(aes(x = date, y = crime_society, group = blm),
              se = FALSE,
              col = 'black',
              size = .3) + 
  geom_vline(xintercept = as.Date("2020-05-29"),
             linetype = 2,
             size = .3) + 
  labs(x = "Date", y = "Society Crime (AUS)",
       title = "Society Crime (AUS)") + 
  theme_tufte(base_size = 9)

crimeplot2 = crime_aus %>% 
  filter(date >= as.Date("2020-03-23")) %>%
  filter(blmrv <= 67) %>% 
  ggplot() + 
  geom_point(aes(x = date, y = crime_property),
             size = .3,
             alpha = .3) + 
  geom_smooth(aes(x = date, y = crime_property, group = blm),
              se = FALSE,
              col = 'black',
              size = .3) + 
  geom_vline(xintercept = as.Date("2020-05-29"),
             linetype = 2,
             size = .3) + 
  labs(x = "Date", y = "Property Crime (AUS)",
       title = "Property Crime (AUS)") + 
  theme_tufte(base_size = 9)

crimeplot3 = crime_aus %>% 
  filter(date >= as.Date("2020-03-23")) %>%
  filter(blmrv <= 67) %>% 
  ggplot() + 
  geom_point(aes(x = date, y = crime_person),
             size = .3,
             alpha = .3) + 
  geom_smooth(aes(x = date, y = crime_person, group = blm),
              se = FALSE,
              col = 'black',
              size = .3) + 
  geom_vline(xintercept = as.Date("2020-05-29"),
             linetype = 2,
             size = .3) + 
  labs(x = "Date", y = "Violent Crime (AUS)",
       title = "Violent Crime (AUS)") + 
  theme_tufte(base_size = 9)

# los angeles

crimeplot4 = crime_la %>% 
  filter(date >= as.Date("2020-03-23")) %>%
  filter(blmrv <= 67) %>% 
  ggplot() + 
  geom_point(aes(x = date, y = crime_society),
             size = .3,
             alpha = .3) + 
  geom_smooth(aes(x = date, y = crime_society, group = blm),
              se = FALSE,
              col = 'black',
              size = .3) + 
  geom_vline(xintercept = as.Date("2020-05-29"),
             linetype = 2,
             size = .3) + 
  labs(x = "Date", y = "Society Crime (LA)",
       title = "Society Crime (LA)") + 
  theme_tufte(base_size = 9)

crimeplot5 = crime_la %>% 
  filter(date >= as.Date("2020-03-23")) %>%
  filter(blmrv <= 67) %>% 
  ggplot() + 
  geom_point(aes(x = date, y = crime_property),
             size = .3,
             alpha = .3) + 
  geom_smooth(aes(x = date, y = crime_property, group = blm),
              se = FALSE,
              col = 'black',
              size = .3) + 
  geom_vline(xintercept = as.Date("2020-05-29"),
             linetype = 2,
             size = .3) + 
  labs(x = "Date", y = "Property Crime (LA)",
       title = "Property Crime (LA)") + 
  theme_tufte(base_size = 9)

crimeplot6 = crime_la %>% 
  filter(date >= as.Date("2020-03-23")) %>%
  filter(blmrv <= 67) %>% 
  ggplot() + 
  geom_point(aes(x = date, y = crime_violent),
             size = .3,
             alpha = .3) + 
  geom_smooth(aes(x = date, y = crime_violent, group = blm),
              se = FALSE,
              col = 'black',
              size = .3) + 
  geom_vline(xintercept = as.Date("2020-05-29"),
             linetype = 2,
             size = .3) + 
  labs(x = "Date", y = "Violent Crime (LA)",
       title = "Violent Crime (LA)") + 
  theme_tufte(base_size = 9)

# philly 

crimeplot7 = crime_phi %>% 
  filter(date >= as.Date("2020-03-23")) %>%
  filter(blmrv <= 67) %>% 
  ggplot() + 
  geom_point(aes(x = date, y = crime_society),
             size = .3,
             alpha = .3) + 
  geom_smooth(aes(x = date, y = crime_society, group = blm),
              se = FALSE,
              col = 'black',
              size = .3) + 
  geom_vline(xintercept = as.Date("2020-05-29"),
             linetype = 2,
             size = .3) + 
  labs(x = "Date", y = "Society Crime (PHL)",
       title = "Society Crime (PHL)") + 
  theme_tufte(base_size = 9)

crimeplot8 = crime_phi %>% 
  filter(date >= as.Date("2020-03-23")) %>%
  filter(blmrv <= 67) %>% 
  ggplot() + 
  geom_point(aes(x = date, y = crime_property),
             size = .3,
             alpha = .3) + 
  geom_smooth(aes(x = date, y = crime_property, group = blm),
              se = FALSE,
              col = 'black',
              size = .3) + 
  geom_vline(xintercept = as.Date("2020-05-29"),
             linetype = 2,
             size = .3) + 
  labs(x = "Date", y = "Property Crime (PHL)",
       title = "Property Crime (PHL)") + 
  theme_tufte(base_size = 9)

crimeplot9 = crime_phi %>% 
  filter(date >= as.Date("2020-03-23")) %>%
  filter(blmrv <= 67) %>% 
  ggplot() + 
  geom_point(aes(x = date, y = crime_violent),
             size = .3,
             alpha = .3) + 
  geom_smooth(aes(x = date, y = crime_violent, group = blm),
              se = FALSE,
              col = 'black',
              size = .3) + 
  geom_vline(xintercept = as.Date("2020-05-29"),
             linetype = 2,
             size = .3) + 
  labs(x = "Date", y = "Violent Crime (PHL)",
       title = "Violent Crime (PHL)") + 
  theme_tufte(base_size = 9)

# seattle

crimeplot10 = crime_sea %>% 
  filter(date >= as.Date("2020-03-23")) %>%
  filter(blmrv <= 67) %>% 
  ggplot() + 
  geom_point(aes(x = date, y = cat_society),
             size = .3,
             alpha = .3) + 
  geom_smooth(aes(x = date, y = cat_society, group = blm),
              se = FALSE,
              col = 'black',
              size = .3) + 
  geom_vline(xintercept = as.Date("2020-05-29"),
             linetype = 2,
             size = .3) + 
  labs(x = "Date", y = "Society Crime (SEA)",
       title = "Society Crime (SEA)") + 
  theme_tufte(base_size = 9)

crimeplot11 = crime_sea %>% 
  filter(date >= as.Date("2020-03-23")) %>%
  filter(blmrv <= 67) %>% 
  ggplot() + 
  geom_point(aes(x = date, y = cat_property),
             size = .3,
             alpha = .3) + 
  geom_smooth(aes(x = date, y = cat_property, group = blm),
              se = FALSE,
              col = 'black',
              size = .3) + 
  geom_vline(xintercept = as.Date("2020-05-29"),
             linetype = 2,
             size = .3) + 
  labs(x = "Date", y = "Property Crime (SEA)",
       title = "Property Crime (SEA)") + 
  theme_tufte(base_size = 9)

crimeplot12 = crime_sea %>% 
  filter(date >= as.Date("2020-03-23")) %>%
  filter(blmrv <= 67) %>% 
  ggplot() + 
  geom_point(aes(x = date, y = cat_person),
             size = .3,
             alpha = .3) + 
  geom_smooth(aes(x = date, y = cat_person, group = blm),
              se = FALSE,
              col = 'black',
              size = .3) + 
  geom_vline(xintercept = as.Date("2020-05-29"),
             linetype = 2,
             size = .3) + 
  labs(x = "Date", y = "Violent Crime (SEA)",
       title = "Violent Crime (SEA)") + 
  theme_tufte(base_size = 9)


crime_plot_grob = 
  arrangeGrob(crimeplot1, crimeplot2, crimeplot3,
              crimeplot4, crimeplot5, crimeplot6,
              crimeplot7, crimeplot8, crimeplot9,
              crimeplot10, crimeplot11, crimeplot12,
              ncol = 3)

ggsave(plot = crime_plot_grob, filename = "pics/figure4crime.png", width = 8, height = 1.75*4)

#### generating results plot #### 

# rescaling 

crime_aus[, c("crime_violent_std", "crime_property_std", "crime_society_std")] = 
  crime_aus[, c("crime_person", "crime_property", "crime_society")] %>% 
  apply(X = ., MARGIN = 2, FUN = function(x) 
    (x - mean(x, na.rm = TRUE)) / sd(x, na.rm = TRUE))

crime_la[, c("crime_violent_std", "crime_property_std", "crime_society_std")] = 
  crime_la[, c("crime_violent", "crime_property", "crime_society")] %>% 
  apply(X = ., MARGIN = 2, FUN = function(x) 
    (x - mean(x, na.rm = TRUE)) / sd(x, na.rm = TRUE))

crime_phi[, c("crime_violent_std", "crime_property_std", "crime_society_std")] = 
  crime_phi[, c("crime_violent", "crime_property", "crime_society")] %>% 
  apply(X = ., MARGIN = 2, FUN = function(x) 
    (x - mean(x, na.rm = TRUE)) / sd(x, na.rm = TRUE))

crime_sea[, c("crime_violent_std", "crime_property_std", "crime_society_std")] = 
  crime_sea[, c("cat_person", "cat_property", "cat_society")] %>% 
  apply(X = ., MARGIN = 2, FUN = function(x) 
    (x - mean(x, na.rm = TRUE)) / sd(x, na.rm = TRUE))

# looping 

crime_df_list = list(crime_aus, crime_la, crime_phi, crime_sea)
crime_df_list_out = as.list(rep(NA, length(crime_df_list)))
outcome = c("crime_violent_std", "crime_property_std", "crime_society_std")

for (i in 1:length(crime_df_list)) {
  
  print(paste0("I iteration ", i))
  
  outcome_list = as.list(rep(NA, length(outcome)))
  
  for (k in 1:length(outcome)) {
    
    print(paste0("K iteration ", k))
    
    outcome_list[[k]] = 
      rdrobust(x = (crime_df_list[[i]] %>% as.data.frame)[, 'blmrv'],
               y = (crime_df_list[[i]] %>% as.data.frame)[, outcome[k]],
               kernel = "uni", p = 1, all = TRUE)
    
  }
  
  crime_df_list_out[[i]] = outcome_list
  
}

df_crime_plot = 
  data.frame(
    
    est = c(crime_df_list_out[[1]][[1]]$coef[1],
            crime_df_list_out[[1]][[2]]$coef[1],
            crime_df_list_out[[1]][[3]]$coef[1],
            crime_df_list_out[[2]][[1]]$coef[1],
            crime_df_list_out[[2]][[2]]$coef[1],
            crime_df_list_out[[2]][[3]]$coef[1],
            crime_df_list_out[[3]][[1]]$coef[1],
            crime_df_list_out[[3]][[2]]$coef[1],
            crime_df_list_out[[3]][[3]]$coef[1],
            crime_df_list_out[[4]][[1]]$coef[1],
            crime_df_list_out[[4]][[2]]$coef[1],
            crime_df_list_out[[4]][[3]]$coef[1]),
    
    se = c(crime_df_list_out[[1]][[1]]$se[3],
           crime_df_list_out[[1]][[2]]$se[3],
           crime_df_list_out[[1]][[3]]$se[3],
           crime_df_list_out[[2]][[1]]$se[3],
           crime_df_list_out[[2]][[2]]$se[3],
           crime_df_list_out[[2]][[3]]$se[3],
           crime_df_list_out[[3]][[1]]$se[3],
           crime_df_list_out[[3]][[2]]$se[3],
           crime_df_list_out[[3]][[3]]$se[3],
           crime_df_list_out[[4]][[1]]$se[3],
           crime_df_list_out[[4]][[2]]$se[3],
           crime_df_list_out[[4]][[3]]$se[3]),
    
    pv = c(crime_df_list_out[[1]][[1]]$pv[3],
           crime_df_list_out[[1]][[2]]$pv[3],
           crime_df_list_out[[1]][[3]]$pv[3],
           crime_df_list_out[[2]][[1]]$pv[3],
           crime_df_list_out[[2]][[2]]$pv[3],
           crime_df_list_out[[2]][[3]]$pv[3],
           crime_df_list_out[[3]][[1]]$pv[3],
           crime_df_list_out[[3]][[2]]$pv[3],
           crime_df_list_out[[3]][[3]]$pv[3],
           crime_df_list_out[[4]][[1]]$pv[3],
           crime_df_list_out[[4]][[2]]$pv[3],
           crime_df_list_out[[4]][[3]]$pv[3]),
    
    crime_type = factor(rep(c("Crime (Violent)",
                              "Crime (Property)",
                              "Crime (Society)"), 4),
                        levels = c("Crime (Violent)",
                                   "Crime (Property)",
                                   "Crime (Society)")),
    
    city = factor(c(rep("AUS", 3), rep("LA", 3), rep("PHL", 3), rep("SEA", 3)),
                  levels = c("AUS","LA", "PHL", "SEA"))
    
    
  )


df_crime_plot_violent = df_crime_plot %>% filter(crime_type == "Crime (Violent)")
meta_res_violent = 
  metagen(TE = df_crime_plot_violent$est,
          seTE = df_crime_plot_violent$se, 
          sm = "SMD")
df_crime_plot_property = df_crime_plot %>% filter(crime_type == "Crime (Property)")
meta_res_property = 
  metagen(TE = df_crime_plot_property$est,
          seTE = df_crime_plot_property$se, 
          sm = "SMD")
df_crime_plot_society = df_crime_plot %>% filter(crime_type == "Crime (Society)")
meta_res_society = 
  metagen(TE = df_crime_plot_society$est,
          seTE = df_crime_plot_society$se, 
          sm = "SMD")

df_crime_plot = bind_rows(
  df_crime_plot,
  data.frame(
    est = meta_res_society$TE.random,
    se = meta_res_society$seTE.random,
    pv = meta_res_society$pval.random,
    crime_type = "Crime (Society)",
    city =  "Meta-Analysis"
  ),
  data.frame(
    est = meta_res_property$TE.random,
    se = meta_res_property$seTE.random,
    pv = meta_res_property$pval.random,
    crime_type = "Crime (Property)",
    city =  "Meta-Analysis"
  ),
  data.frame(
    est = meta_res_violent$TE.random,
    se = meta_res_violent$seTE.random,
    pv = meta_res_violent$pval.random,
    crime_type = "Crime (Violent)",
    city =  "Meta-Analysis"
  )
)

df_crime_plot$city = 
  factor(df_crime_plot$city,
         levels = c("AUS",  "LA", "PHL", "SEA", "Meta-Analysis"))

df_crime_plot %>% 
  ggplot() + 
  geom_point(aes(x = city, y = est,
                 shape = crime_type),
             position = position_dodge(.3),
             size = 2) + 
  geom_errorbar(aes(x = city, 
                    ymin = est - 1.96 * se,
                    ymax = est + 1.96 * se, 
                    shape = crime_type),
                position = position_dodge(.3),
                width = 0,
                size = .2) + 
  geom_errorbar(aes(x = city, 
                    ymin = est - 1.645 * se,
                    ymax = est + 1.645 * se,
                    shape = crime_type),
                position = position_dodge(.3),
                width = 0,
                size = .4) + 
  geom_hline(yintercept = 0, size = .4) + 
  labs(x = "City", y = "Standardized Coefficient\n(BLM Protest)",
       shape = "Outcome") + 
  theme_tufte()

ggsave(plot = last_plot(), width = 6, height = 2.25,
       filename = "pics/figure5crime.png")

#### MAKE REGRESSION TABLE FOR APPENDIX ####

library(xtable)

# austin rows

atx_violent <- as.data.frame(cbind(crime_df_list_out[[1]][[1]]$coef[1],
                                   crime_df_list_out[[1]][[1]]$se[3],
                                   crime_df_list_out[[1]][[1]]$pv[3],
                                   crime_df_list_out[[1]][[1]]$N[1],
                                   (crime_df_list_out[[1]][[1]]$bws[1]*2),
                                   crime_df_list_out[[1]][[1]]$bws[1]))

atx_property <- as.data.frame(cbind(crime_df_list_out[[1]][[2]]$coef[1],
                                    crime_df_list_out[[1]][[2]]$se[3],
                                    crime_df_list_out[[1]][[2]]$pv[3],
                                    crime_df_list_out[[1]][[2]]$N[1],
                                    (crime_df_list_out[[1]][[2]]$bws[1]*2),
                                    crime_df_list_out[[1]][[2]]$bws[1]))

atx_society <- as.data.frame(cbind(crime_df_list_out[[1]][[3]]$coef[1],
                                   crime_df_list_out[[1]][[3]]$se[3],
                                   crime_df_list_out[[1]][[3]]$pv[3],
                                   crime_df_list_out[[1]][[3]]$N[1],
                                   (crime_df_list_out[[1]][[3]]$bws[1]*2),
                                   crime_df_list_out[[1]][[3]]$bws[1]))

# LA rows
LA_violent <- as.data.frame(cbind(crime_df_list_out[[2]][[1]]$coef[1],
                                  crime_df_list_out[[2]][[1]]$se[3],
                                  crime_df_list_out[[2]][[1]]$pv[3],
                                  crime_df_list_out[[2]][[1]]$N[1],
                                  (crime_df_list_out[[2]][[1]]$bws[1]*2),
                                  crime_df_list_out[[2]][[1]]$bws[1]))

LA_property <- as.data.frame(cbind(crime_df_list_out[[2]][[2]]$coef[1],
                                   crime_df_list_out[[2]][[2]]$se[3],
                                   crime_df_list_out[[2]][[2]]$pv[3],
                                   crime_df_list_out[[2]][[2]]$N[1],
                                   (crime_df_list_out[[2]][[2]]$bws[1]*2),
                                   crime_df_list_out[[2]][[2]]$bws[1]))

LA_society <- as.data.frame(cbind(crime_df_list_out[[2]][[3]]$coef[1],
                                  crime_df_list_out[[2]][[3]]$se[3],
                                  crime_df_list_out[[2]][[3]]$pv[3],
                                  crime_df_list_out[[2]][[3]]$N[1],
                                  (crime_df_list_out[[2]][[3]]$bws[1]*2),
                                  crime_df_list_out[[2]][[3]]$bws[1]))


# Philly rows
philly_violent <- as.data.frame(cbind(crime_df_list_out[[3]][[1]]$coef[1],
                                      crime_df_list_out[[3]][[1]]$se[3],
                                      crime_df_list_out[[3]][[1]]$pv[3],
                                      crime_df_list_out[[3]][[1]]$N[1],
                                      (crime_df_list_out[[3]][[1]]$bws[1]*2),
                                      crime_df_list_out[[3]][[1]]$bws[1]))

philly_property <- as.data.frame(cbind(crime_df_list_out[[3]][[2]]$coef[1],
                                       crime_df_list_out[[3]][[2]]$se[3],
                                       crime_df_list_out[[3]][[2]]$pv[3],
                                       crime_df_list_out[[3]][[2]]$N[1],
                                       (crime_df_list_out[[3]][[2]]$bws[1]*2),
                                       crime_df_list_out[[3]][[2]]$bws[1]))

philly_society <- as.data.frame(cbind(crime_df_list_out[[3]][[3]]$coef[1],
                                      crime_df_list_out[[3]][[3]]$se[3],
                                      crime_df_list_out[[3]][[3]]$pv[3],
                                      crime_df_list_out[[3]][[3]]$N[1],
                                      (crime_df_list_out[[3]][[3]]$bws[1]*2),
                                      crime_df_list_out[[3]][[3]]$bws[1]))

#Seattle rows
sea_violent <- as.data.frame(cbind(crime_df_list_out[[4]][[1]]$coef[1],
                                   crime_df_list_out[[4]][[1]]$se[3],
                                   crime_df_list_out[[4]][[1]]$pv[3],
                                   crime_df_list_out[[4]][[1]]$N[1],
                                   (crime_df_list_out[[4]][[1]]$bws[1]*2),
                                   crime_df_list_out[[4]][[1]]$bws[1]))

sea_property <- as.data.frame(cbind(crime_df_list_out[[4]][[2]]$coef[1],
                                    crime_df_list_out[[4]][[2]]$se[3],
                                    crime_df_list_out[[4]][[2]]$pv[3],
                                    crime_df_list_out[[4]][[2]]$N[1],
                                    (crime_df_list_out[[4]][[2]]$bws[1]*2),
                                    crime_df_list_out[[4]][[2]]$bws[1]))

sea_society <- as.data.frame(cbind(crime_df_list_out[[4]][[3]]$coef[1],
                                   crime_df_list_out[[4]][[3]]$se[3],
                                   crime_df_list_out[[4]][[3]]$pv[3],
                                   crime_df_list_out[[4]][[3]]$N[1],
                                   (crime_df_list_out[[4]][[3]]$bws[1]*2),
                                   crime_df_list_out[[4]][[3]]$bws[1]))


tab <- as.data.frame(rbind(atx_violent,atx_property,atx_society,
                           LA_violent,LA_property,LA_society,
                           philly_violent,philly_property,philly_society,
                           sea_violent,sea_property,sea_society))

tab <- tab %>% mutate(V4 = as.character(V4))
tab <- tab %>% mutate_if(is.numeric,round, digits = 2)

cities <- as.data.frame(rbind("Austin","Austin","Austin",
                              "LA","LA","LA",
                              "Philly","Philly","Philly",
                              "Seattle","Seattle","Seattle"))
outcome <- as.data.frame(rbind("Violent","Property","Society",
                               "Violent","Property","Society",
                               "Violent","Property","Society",
                               "Violent","Property","Society"))

tab <- as.data.frame(cbind(cities,outcome,tab))
colnames(tab) <- c("City","Crime Type","Coeff","SE","P-Val","N-Val","Effective N","Bandwidth Est.")

xt <- xtable(
  x = tab,
  align = "lllcccccc",
  type = "html",
  caption = "RDiT Coefficients Characterizing the Effect of BLM Protests on Crime.",
  label = "tab:crime_reg_tab"
)


addtorow <- list()
addtorow$pos <- list(nrow(xt))
addtorow$command <- as.vector(c(paste("\\hline\n\\multicolumn{8}{l}{\\emph{All estimates are specified with a uniform kernel and polynomial degree equal to 1.}}\\\\\n",
                                      paste("\\multicolumn{8}{l}{\\emph{Standard errors are robust.}}\\\\\n",
                                            sep=""))))

print(xt,
      format.args = list(big.mark = ","),
      caption.placement = "top",
      include.rownames = FALSE,
      add.to.row = addtorow,
      file = "fig6_reg_tab.tex",
      hline.after = c(-1, 0, 12)
)








